---
--- Created by 旅行者.
--- DateTime: 2025/2/18 22:56
---
local JIANMU_SAPLING = TUNING.ENTITY_LIST.JIANMU_SAPLING
local JIANMU = TUNING.ENTITY_LIST.JIANMU
local assets = {
    Asset("ANIM", JIANMU_SAPLING.anim),  -- 使用原版树苗的动画
    Asset("ATLAS", JIANMU_SAPLING.atlasPath),  -- 使用自定义的贴图
    Asset("IMAGE", JIANMU_SAPLING.imagePath),
}

local function onDeploy(inst, pt, deployer)
    local tree = SpawnPrefab(JIANMU.id)  -- 暂时使用原版的树作为种植后的物品
    if tree ~= nil then
        tree.Transform:SetPosition(pt:Get())
        -- 如果树苗有 i26_jianmu_data 组件，将其状态传递给新生成的建木
        if inst.components.i26_jianmu_data then
            tree.components.i26_jianmu_data:OnLoad(inst.components.i26_jianmu_data:OnSave())
        end
        inst:Remove()  -- 移除树苗物品
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank(JIANMU_SAPLING.id)
    inst.AnimState:SetBuild(JIANMU_SAPLING.scmlName)
    inst.AnimState:PlayAnimation("idle")

    inst:AddTag("tornado_nosucky") --mod兼容：永不妥协。不会被龙卷风刮走
    inst:AddTag("deployedplant")

    if not TheWorld.ismastersim then
        return inst
    end

    inst.entity:SetPristine()

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = JIANMU_SAPLING.atlasPath
    inst.components.inventoryitem.imagename = JIANMU_SAPLING.id  -- 指定自定义的贴图

    -- 添加 i26_jianmu_data 组件，用于存储状态数据
    inst:AddComponent("i26_jianmu_data")
    inst.components.i26_jianmu_data.is_sapling = true  -- 设置为树苗

    inst:AddComponent("deployable")
    inst.components.deployable.ondeploy = onDeploy
    inst.components.deployable:SetDeployMode(DEPLOYMODE.PLANT)
    inst.components.deployable:SetDeploySpacing(DEPLOYSPACING.MEDIUM)


    return inst
end

return Prefab(JIANMU_SAPLING.id, fn, assets),
MakePlacer("i26_jianmu_sapling_placer", JIANMU.id, JIANMU.scmlName, "idle2")